package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.CancelMallOrderReq;
import com.ydkj.a37e_mall.Classes.Model.Request.DeleteMallOrderReq;
import com.ydkj.a37e_mall.Classes.Model.Request.DeleteOrderReq;
import com.ydkj.a37e_mall.Classes.Model.Request.GetExpressReq;
import com.ydkj.a37e_mall.Classes.Model.Response.GetExpressResp;
import com.ydkj.a37e_mall.Classes.Model.Response.GetMallOrderListResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;
import com.ydkj.a37e_mall.Resource.View.TimerTextView;

import org.json.JSONObject;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.ydkj.a37e_mall.R.id.lly_all;
import static com.ydkj.a37e_mall.R.id.lly_all_courier_number;
import static com.ydkj.a37e_mall.R.id.tv_courier_number;
import static com.ydkj.a37e_mall.R.id.tv_delete_order;
import static com.ydkj.a37e_mall.R.id.tv_logistics_company;
import static com.ydkj.a37e_mall.R.id.tv_logistics_status;
import static com.ydkj.a37e_mall.R.id.tv_logistics_track;
import static com.ydkj.a37e_mall.R.id.tv_status;

/**
 * Created by Administrator on 2017/2/28.
 * 订单详情
 */

public class OrderDetail extends BaseActivity implements View.OnClickListener {

    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private List<GetMallOrderListResp.ResultEntity.DataEntity> intentList;
    private List<GetExpressResp.ResultBean.DataBean> logisticsList;

    private ImageView img_back, img_logistics;
    private LinearLayout lly_logistics_information;
    private ListView lv_mall_order_detail;
    private TextView tv_logistics_detail, tv_logistics_time, tv_receive_name, tv_phone_number, tv_address;
    private TextView tv_order_sn, tv_create_time, tv_pay_time, tv_delivery_time;
    private TimerTextView tv_comfirm_receive_time;
    private AllProductOrderAdapter allProductOrderAdapter;
    private String secret;
    private long days, hours, minutes, seconds;

    private RelativeLayout rly_had_complete; //订单状态按钮判断
    private TextView tv_see_logistics, tv_confirm_receipt;


    private LinearLayout lly_request;//填写快递单号
    private EditText et_order;
    private TextView tv_delete_order1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mall_order_detail);
        mActivity = this;
        init();
        initWindow();//沉浸式状态栏
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.e50011), 0);
    }


    private void init() {

        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        myApplication = MyApplication.getInstance();
        intentList = new ArrayList<GetMallOrderListResp.ResultEntity.DataEntity>();
        logisticsList = new ArrayList<GetExpressResp.ResultBean.DataBean>();

        intentList.clear();
        if (this.getIntent().getSerializableExtra("intentList") != null) {
            intentList = (ArrayList<GetMallOrderListResp.ResultEntity.DataEntity>) getIntent().getSerializableExtra("intentList");
            Log.e("ssss", intentList.get(0).getId());
        }
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);

        rly_had_complete = (RelativeLayout) mActivity.findViewById(R.id.rly_had_complete);
        tv_see_logistics = (TextView) mActivity.findViewById(tv_delete_order);
        tv_confirm_receipt = (TextView) mActivity.findViewById(R.id.tv_see_logistics);


        lly_request = (LinearLayout) mActivity.findViewById(R.id.lly_request);
        et_order = (EditText) mActivity.findViewById(R.id.et_order);
        tv_delete_order1 = (TextView) mActivity.findViewById(R.id.tv_delete_order1);


        img_logistics = (ImageView) mActivity.findViewById(R.id.img_logistics);
        lv_mall_order_detail = (ListView) mActivity.findViewById(R.id.lv_mall_order_detail);
        lly_logistics_information = (LinearLayout) mActivity.findViewById(R.id.lly_logistics_information);
        tv_logistics_detail = (TextView) mActivity.findViewById(R.id.tv_logistics_detail);
        tv_logistics_time = (TextView) mActivity.findViewById(R.id.tv_logistics_time);
        tv_receive_name = (TextView) mActivity.findViewById(R.id.tv_receive_name);
        tv_phone_number = (TextView) mActivity.findViewById(R.id.tv_phone_number);
        tv_address = (TextView) mActivity.findViewById(R.id.tv_address);
        tv_comfirm_receive_time = (TimerTextView) mActivity.findViewById(R.id.tv_comfirm_receive_time);
        tv_order_sn = (TextView) mActivity.findViewById(R.id.tv_order_sn);
        tv_create_time = (TextView) mActivity.findViewById(R.id.tv_create_time);
        tv_pay_time = (TextView) mActivity.findViewById(R.id.tv_pay_time);
        tv_delivery_time = (TextView) mActivity.findViewById(R.id.tv_delivery_time);

        allProductOrderAdapter = new AllProductOrderAdapter(intentList);
        lv_mall_order_detail.setAdapter(allProductOrderAdapter);

        if (intentList.get(0).getStatus_id().equals("5") || intentList.get(0).getStatus_id().equals("10")) {
            img_logistics.setImageDrawable(getResources().getDrawable(R.mipmap.logistics_orange));
            GetExpress();


            try {
                SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化时间
                String nowtime1 = d.format(new Date());// 按以上格式 将当前时间转换成字符串

                Log.e("url", "11111");
                Log.e("url", "收货时间：" + intentList.get(0).getAuto_receive());

                String testtime = intentList.get(0).getAuto_receive();// 测试时间
                long result = (d.parse(testtime).getTime() - d.parse(nowtime1).getTime());// 当前时间减去测试时间
                // 这个的除以1000得到秒，相应的60000得到分，3600000得到小时
                days = result / (3600000 * 24);
                hours = (result - days * (3600000 * 24)) / 3600000;
                minutes = (result - days * (3600000 * 24) - hours * 3600000) / 60000;
                seconds = (result - days * (3600000 * 24) - hours * 3600000 - minutes * 60000) / 1000;

                Log.e("url", "结果：" + result + "\n" + days + "天" + "\n"
                        + hours + "小时" + "\n" + minutes + "分钟" + "\n" + seconds
                        + "秒");
            } catch (Exception e) {
                e.printStackTrace();
            }
            long[] times = {days, hours, minutes, seconds};
            tv_comfirm_receive_time.setTimes(times);
            tv_comfirm_receive_time.beginRun();


//            tv_comfirm_receive_time.setText("自动确认收货时间:" + intentList.get(0).getAuto_receive());
            tv_pay_time.setText("付款时间:" + intentList.get(0).getPay_time());
            tv_delivery_time.setText("发货时间:" + intentList.get(0).getDelivery_time());
        } else {
            img_logistics.setImageDrawable(getResources().getDrawable(R.mipmap.logistics_default));
            tv_logistics_detail.setText("暂无物流消息");
            tv_logistics_detail.setTextColor(getResources().getColor(R.color.A999999));
            tv_logistics_time.setVisibility(View.GONE);

            tv_comfirm_receive_time.setVisibility(View.GONE);
            tv_pay_time.setVisibility(View.GONE);
            tv_delivery_time.setVisibility(View.GONE);
        }

        tv_receive_name.setText("收货人: " + intentList.get(0).getConsignee());
        tv_phone_number.setText(intentList.get(0).getMobile());
        tv_address.setText("收货地址: " + intentList.get(0).getAddress());

        tv_order_sn.setText("订单编号: " + intentList.get(0).getSn());
        tv_create_time.setText("创建时间: " + intentList.get(0).getCreated_at());

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        lly_logistics_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, SeeLogistics.class);
                intent.putExtra("intentList", (Serializable) intentList);
                startActivity(intent);
            }
        });


        tv_see_logistics.setOnClickListener(this);
        tv_confirm_receipt.setOnClickListener(this);
        tv_delete_order1.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case tv_delete_order: //第1个
                if (intentList.get(0).getStatus_id().equals("1")) {
                    //待付款  取消订单
                    ConfirmReceipt(intentList.get(0).getId(), "14", "");
                } else if (intentList.get(0).getStatus_id().equals("2") || intentList.get(0).getStatus_id().equals("3") || intentList.get(0).getStatus_id().equals("4")) {
                    return;
                } else if (intentList.get(0).getStatus_id().equals("5")) {
                    //待收货 查看物流
                    intentList.clear();
                    intentList.add(intentList.get(0));
                    Intent intent = new Intent(mActivity, SeeLogistics.class);
                    intent.putExtra("intentList", (Serializable) intentList);
                    startActivity(intent);
                } else if (intentList.get(0).getStatus_id().equals("6")) {
                    //已完成
                    return;
                } else if (intentList.get(0).getStatus_id().equals("7")) {

                    return;

                } else if (intentList.get(0).getStatus_id().equals("8")) {

                    return;

                } else if (intentList.get(0).getStatus_id().equals("9")) {
                    return;

                } else if (intentList.get(0).getStatus_id().equals("10")) {
                    //换货中 查看物流

                    intentList.clear();
                    intentList.add(intentList.get(0));
                    Intent intent = new Intent(mActivity, SeeLogistics.class);
                    intent.putExtra("intentList", (Serializable) intentList);
                    startActivity(intent);
                } else if (intentList.get(0).getStatus_id().equals("11")) {
                    return;

                }
                break;
            case R.id.tv_see_logistics://第2个
                if (intentList.get(0).getStatus_id().equals("1")) {
                    //待付款  付款
                    int invoice = Integer.parseInt(intentList.get(0).getInvoice());
                    Intent intent = new Intent(mActivity, PayMoney.class);
                    intent.putExtra("order_id", intentList.get(0).getId());
                    intent.putExtra("order_sn", intentList.get(0).getSn());
                    intent.putExtra("order_price", intentList.get(0).getTotal_fee());
                    Log.e("ssss", "付款数==》" + intentList.get(0).getTotal_fee());
                    startActivity(intent);
                } else if (intentList.get(0).getStatus_id().equals("2") || intentList.get(0).getStatus_id().equals("3") || intentList.get(0).getStatus_id().equals("4")) {
                    return;
                } else if (intentList.get(0).getStatus_id().equals("5")) {
                    //待收货 确认收货
                    ConfirmReceipt(intentList.get(0).getId(), "6", "");
                } else if (intentList.get(0).getStatus_id().equals("6")) {
                    //已完成  换货
                    ConfirmReceipt(intentList.get(0).getId(), "7", "");

                } else if (intentList.get(0).getStatus_id().equals("7")) {
                    //换货中 订单号确认
                    ConfirmReceipt(intentList.get(0).getId(), "9", et_order.getText().toString());

                } else if (intentList.get(0).getStatus_id().equals("8")) {
                    //换货中 订单号确认
                    ConfirmReceipt(intentList.get(0).getId(), "9", et_order.getText().toString());

                } else if (intentList.get(0).getStatus_id().equals("9")) {
                    return;

                } else if (intentList.get(0).getStatus_id().equals("10")) {
                    //换货中 确认收货
                    ConfirmReceipt(intentList.get(0).getId(), "6", et_order.getText().toString());

                } else if (intentList.get(0).getStatus_id().equals("11")) {
                    //交易关闭 删除订单
                    DeleteOrder(intentList.get(0).getId());

                }
                break;
            case R.id.tv_delete_order1://订单号确认
                ConfirmReceipt(intentList.get(0).getId(), "9", et_order.getText().toString());

                break;
        }

    }


    public class AllProductOrderAdapter extends BaseAdapter {
        public List<GetMallOrderListResp.ResultEntity.DataEntity> intentList;

        public AllProductOrderAdapter(List<GetMallOrderListResp.ResultEntity.DataEntity> comment) {
            if (comment == null) {
                comment = new ArrayList<GetMallOrderListResp.ResultEntity.DataEntity>();
            }
            intentList = comment;
        }

        @Override
        public int getCount() {
            return intentList.size();
        }

        /**
         * 获得某一位置的数据
         */
        @Override
        public GetMallOrderListResp.ResultEntity.DataEntity getItem(int position) {
            return intentList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.order_all_listitem, null);
                vh = new ViewHolder();
                vh.tv_order_states = (TextView) convertView.findViewById(R.id.tv_order_states);
                vh.tv_order_num = (TextView) convertView.findViewById(R.id.tv_order_num);
                vh.tv_status = (TextView) convertView.findViewById(tv_status);
                vh.lly_all_product_order = (LinearLayout) convertView.findViewById(R.id.lly_all_product_order);
                vh.tv_product_num = (TextView) convertView.findViewById(R.id.tv_product_num);
                vh.tv_all_product_price = (TextView) convertView.findViewById(R.id.tv_all_product_price);
                vh.tv_give_integral = (TextView) convertView.findViewById(R.id.tv_give_integral);
                vh.lly_button = (LinearLayout) convertView.findViewById(R.id.lly_button);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);
            convertView.setClickable(true);

            vh.tv_order_num.setText(getItem(position).getSn());
            vh.tv_product_num.setText("共计" + getItem(position).getNum() + "件商品");//购买数量
            vh.tv_all_product_price.setText(getItem(position).getTotal_fee());//价格

            vh.lly_all_product_order.removeAllViews();
            LinearLayout productLayout = (LinearLayout) LayoutInflater.from(mActivity).inflate(R.layout.mall_order_layout, null);
            ImageView img_thumb = (ImageView) productLayout.findViewById(R.id.img_thumb);
            TextView tv_describe = (TextView) productLayout.findViewById(R.id.tv_describe);
            TextView tv_price = (TextView) productLayout.findViewById(R.id.tv_price);
            TextView tv_postage = (TextView) productLayout.findViewById(R.id.tv_postage);
            TextView tv_type_content = (TextView) productLayout.findViewById(R.id.tv_type_content);
            TextView tv_num = (TextView) productLayout.findViewById(R.id.tv_num);
            tv_describe.setText(intentList.get(position).getInfo());
            tv_price.setText("¥ " + intentList.get(position).getTotal_fee());
            tv_num.setText("X" + intentList.get(position).getNum());


            for (int i = 0; i < intentList.get(position).getProduct().size(); i++) {
                if (i == 0) {
                    tv_type_content.setText(intentList.get(position).getProduct().get(i).getStandard_type() + ":"
                            + intentList.get(position).getProduct().get(i).getStandard_name());
                } else {
                    tv_type_content.setText(tv_type_content.getText().toString() + "\n" + intentList.get(position).getProduct().get(i).getStandard_type() + ":"
                            + intentList.get(position).getProduct().get(i).getStandard_name());
                }
            }

            ImageLoader.getInstance().displayImage(getItem(position).getThumb(), img_thumb, displayImageOptions);


            vh.lly_all_product_order.addView(productLayout);

            rly_had_complete.setVisibility(View.GONE);
            lly_request.setVisibility(View.GONE);

            Log.e("ssss1", "订单状态===》" + getItem(position).getStatus_id());
            tv_see_logistics.setBackground(getResources().getDrawable(R.drawable.round_rectangle_red));//1111
            tv_confirm_receipt.setBackground(getResources().getDrawable(R.drawable.round_rectangle_red));//2222
            if (getItem(position).getStatus_id().equals("1")) {
                rly_had_complete.setVisibility(View.VISIBLE);
                vh.tv_status.setText("待付款");

                tv_see_logistics.setText("取消订单");
                tv_confirm_receipt.setText("付款");

            } else if (getItem(position).getStatus_id().equals("2") || getItem(position).getStatus_id().equals("3") || getItem(position).getStatus_id().equals("4")) {
                vh.tv_status.setText("商家未发货");
                rly_had_complete.setVisibility(View.VISIBLE);

                tv_see_logistics.setText("查看物流");
                tv_confirm_receipt.setText("确认收货");
                tv_see_logistics.setBackground(getResources().getDrawable(R.drawable.round_search_gray));
                tv_confirm_receipt.setBackground(getResources().getDrawable(R.drawable.round_search_gray));
            } else if (getItem(position).getStatus_id().equals("5")) {
                vh.tv_status.setText("待收货");
                rly_had_complete.setVisibility(View.VISIBLE);

                tv_see_logistics.setText("查看物流");
                tv_confirm_receipt.setText("确认收货");

            } else if (getItem(position).getStatus_id().equals("6")) {

                vh.tv_status.setText("已完成");
                rly_had_complete.setVisibility(View.VISIBLE);
                tv_see_logistics.setVisibility(View.GONE);
                tv_confirm_receipt.setText("换货");

            } else if (getItem(position).getStatus_id().equals("7")) {
                lly_request.setVisibility(View.VISIBLE);

                vh.tv_status.setText("待审核");
                //申请换货
                tv_see_logistics.setVisibility(View.GONE);
                tv_confirm_receipt.setVisibility(View.GONE);


            } else if (getItem(position).getStatus_id().equals("8")) {

                vh.tv_status.setText("待审核");

                //等待买家发货
                tv_see_logistics.setVisibility(View.GONE);
                tv_confirm_receipt.setVisibility(View.GONE);
                lly_request.setVisibility(View.VISIBLE);


            } else if (getItem(position).getStatus_id().equals("9")) {

                rly_had_complete.setVisibility(View.VISIBLE);
                vh.tv_status.setText("审核");
                tv_see_logistics.setText("查看物流");
                tv_confirm_receipt.setText("确认收货");
                //等待卖家收货
                tv_see_logistics.setVisibility(View.VISIBLE);
                tv_confirm_receipt.setVisibility(View.VISIBLE);

                tv_see_logistics.setBackground(getResources().getDrawable(R.drawable.round_search_gray));
                tv_confirm_receipt.setBackground(getResources().getDrawable(R.drawable.round_search_gray));

            } else if (getItem(position).getStatus_id().equals("10")) {

                rly_had_complete.setVisibility(View.VISIBLE);
                vh.tv_status.setText("审核成功");
                tv_see_logistics.setText("查看物流");
                tv_confirm_receipt.setText("确认收货");
                //卖家已换货
                tv_see_logistics.setVisibility(View.VISIBLE);
                tv_see_logistics.setVisibility(View.VISIBLE);
                tv_confirm_receipt.setVisibility(View.VISIBLE);


            } else if (getItem(position).getStatus_id().equals("11")) {
                rly_had_complete.setVisibility(View.VISIBLE);
                vh.tv_status.setText("交易关闭");
                tv_see_logistics.setVisibility(View.VISIBLE);
                tv_see_logistics.setVisibility(View.GONE);
                tv_confirm_receipt.setText("删除订单");

            } else {
//                lly_all.setVisibility(View.GONE);

            }

            return convertView;
        }

        public class ViewHolder {
            public TextView tv_order_states, tv_order_num, tv_status;
            private LinearLayout lly_all_product_order, lly_button;
            private TextView tv_product_num, tv_all_product_price, tv_give_integral;
        }
    }

//    //取消订单
//    private void CancelOrder(int id, String status) {
//        CustomProgressDialog.createDialog(mActivity).show();
//        CancelMallOrderReq req = new CancelMallOrderReq();
//        req.setStatus(status);
//        req.setAccess_token(sharedPreferencesUtil.getAccessToken());
//        try {
//            String source = req.getString();
//            Rsa rsa = new Rsa();
//            secret = rsa.encryptByPublic(source);
//            Log.e("url", "source==" + source);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        RequestParams params = new RequestParams();
//        params.put("status", req.getStatus());
//        params.put("access_token", req.getAccess_token());
//        params.put("secret", secret);
//        Log.e("url", "params==" + params);
//        String url = Const.COMMON_ADDRESS + req.urlString() + id;
//        AsyncHttpClient client = new AsyncHttpClient();
//        client.put(url, params, new JsonHttpResponseHandler() {
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//                super.onSuccess(statusCode, headers, response);
//                CustomProgressDialog.Dismiss();
//                Log.e("url", "CancelOrder_json==" + response);
//                String errno = response.optString("errno");
//                String errmsg = response.optString("errmsg");
//                if (errno.equals("000")) {
////                    GetAllOrderList(REFRESH);
//                    finish();
//                    CommonUtil.showToast(mActivity, errmsg);
//                } else if (errno.equals("999")) {
//                    CommonUtil.showToast(mActivity, errmsg);
//                    Intent intent = new Intent(mActivity, LoginActivity.class);
//                    startActivityForResult(intent, 1);
//                    Log.e("url", "errmsg==" + errmsg);
//                }
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                super.onFailure(statusCode, headers, responseString, throwable);
//                CustomProgressDialog.Dismiss();
//                CommonUtil.showToast(mActivity, "网络连接失败");
//            }
//        });
//    }

    //确认收货
//    private void ConfirmReceipt(String id, String status) {
//        CustomProgressDialog.createDialog(mActivity).show();
//        CancelMallOrderReq req = new CancelMallOrderReq();
//        req.setStatus(status);
//        req.setAccess_token(sharedPreferencesUtil.getAccessToken());
//        try {
//            String source = req.getString();
//            Rsa rsa = new Rsa();
//            secret = rsa.encryptByPublic(source);
//            Log.e("url", "source==" + source);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        RequestParams params = new RequestParams();
//        params.put("status", req.getStatus());
//        params.put("access_token", req.getAccess_token());
//        params.put("secret", secret);
//        Log.e("url", "params==" + params);
//        String url = Const.COMMON_ADDRESS + req.urlString() + id;
//        AsyncHttpClient client = new AsyncHttpClient();
//        client.put(url, params, new JsonHttpResponseHandler() {
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//                super.onSuccess(statusCode, headers, response);
//                CustomProgressDialog.Dismiss();
//                Log.e("url", "ConfirmReceipt_json==" + response);
//                String errno = response.optString("errno");
//                String errmsg = response.optString("errmsg");
//                if (errno.equals("000")) {
//                    finish();
//                    CommonUtil.showToast(mActivity, errmsg);
//                } else if (errno.equals("999")) {
//                    CommonUtil.showToast(mActivity, errmsg);
//                    Intent intent = new Intent(mActivity, LoginActivity.class);
//                    startActivityForResult(intent, 1);
//                    Log.e("url", "errmsg==" + errmsg);
//                }
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                super.onFailure(statusCode, headers, responseString, throwable);
//                CustomProgressDialog.Dismiss();
//                CommonUtil.showToast(mActivity, "网络连接失败");
//            }
//        });
//    }


    //修改订单状态
    private void ConfirmReceipt(String id, String stats, String text) {
        CustomProgressDialog.createDialog(mActivity).show();
        CancelMallOrderReq req = new CancelMallOrderReq();
        req.setUuid(myApplication.getDataEntity().getUuid());
        req.setReturned_delivery(text);
        req.setDelivery_no("");
        req.setStats(stats);

        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", req.getSecret());


        String url = Const.COMMON_ADDRESS + req.urlString() + id;
        AsyncHttpClient client = new AsyncHttpClient();
        client.put(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("url", "cancel_order_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);

                } else if (errno.equals("999")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 1);
                    Log.e("url", "errmsg==" + errmsg);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                    CustomProgressDialog.Dismiss();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    //删除订单
    private void DeleteOrder(String id) {
        CustomProgressDialog.createDialog(mActivity).show();
        DeleteOrderReq req = new DeleteOrderReq();

        req.setUuid(myApplication.getDataEntity().getUuid());

        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", req.getSecret());


        String url = Const.COMMON_ADDRESS + req.urlString() + id;
        AsyncHttpClient client = new AsyncHttpClient();
        client.put(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("url", "cancel_order_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);

                } else if (errno.equals("999")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 1);
                    Log.e("url", "errmsg==" + errmsg);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                    CustomProgressDialog.Dismiss();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    /**
     * 获取快递情况
     */
    private void GetExpress() {

        GetExpressReq req = new GetExpressReq();
        req.setUuid(myApplication.getDataEntity().getUuid());
        req.setId(intentList.get(0).getId());
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
            Log.e("url", "source==" + source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = Const.COMMON_ADDRESS + req.urlString();
        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", secret);
        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("url", "all_mall_order_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    GetExpressResp resp = GsonImpl.get().toObject(response.toString(), GetExpressResp.class);
                    logisticsList.clear();
                    logisticsList.add(resp.getResult().getData());

                    if (logisticsList.size() != 0) {
                        tv_logistics_detail.setText(logisticsList.get(0).getDetail().get(0).getContext());
                        tv_logistics_time.setText(logisticsList.get(0).getDetail().get(0).getTime());

                    } else {
                        tv_logistics_detail.setText("暂无物流消息");
                        tv_logistics_time.setVisibility(View.GONE);
                    }

//                    setAdapter();
                } else if (errno.equals("999")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 3);
                    Log.e("url", "errmsg==" + errmsg);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }
}
